#include "PassNearGoalBehaviour.h"
#include "../../CommonFunctions.h"

/*
	Spravanie brankara v pripade prihravky nachadzajucej sa v blizkosti branky
 */
bool PassNearGoalBehaviour::Behave()
{
//		G_TXTN("  LPT_PassNearGoal");
	Point home = GetInterceptBallBehaviour()->GetNearestBallInterceptPos();
	if(home.GetX() != -1000)
	{
		Ball tmpBall(*world->ball); tmpBall.Step();
		
		Vector meHome = (home - world->me->pos);
		Vector meBall = (tmpBall.pos - world->me->pos);
		float deg; // = fabsf(meHome.GetAngle().GetDeg() - meBall.GetAngle().GetDeg());
		deg = fabsf(meHome.GetAngle().GetDeg() - world->me->GetAbsDir().GetDeg());
		if(deg > 180) deg = 360 - deg;
		if(deg < 110 || (world->me->pos - home).GetLength() > 15) GetRunToPositionBehaviour()->Behave(home, true, true, 0, 40);
		else GetRunToPositionBehaviour()->BehaveBackwards(home, true, true, 0, 40);
		DBGN("Running to (%.2f, %.2f)", home.GetX(), home.GetY());
		return true;
	}
	return false;
}

// Simulovanie pozicii lopty v najblizsich 30 cykloch
// Ak lopta dosiahne nase pokutove uzemie a stale ma rychlost => je to nahravka
// POZN.: Tato funkcia sa mi nepaci! Ako dosli k parametru 0.3? 
bool PassNearGoalBehaviour::IsPassNearGoal()
{
	int cycle;
	Ball tmpBall(*world->ball);

	for(cycle = 0; cycle < 30; cycle++)
	{
		if(tmpBall.vel.GetLength() < world->params->server->ball_speed_max * 0.3)
			return false;
		
		if(DATA_STORAGE.InPenaltyArea(tmpBall.pos))
			return true;
		
		tmpBall.Step();
	}

	return false;
}
